#############################################
################################################################################################
rm(list=ls()) 
x=c("lfe","tidyr","broom","ggplot2")

lapply(x, library, character.only=TRUE) # load the required packages
library(dplyr)
library(starpolishr)
library(data.table)


setwd("Derived Data")

# from createbartik2005.R
dfdist=fread("distanalysisleaid.csv")

dfpol=fread("df_pol_diffplot.csv")

dfpol=dfpol %>% filter(testmonth=="May")

coal=dfdist[dfdist$fuel=="coal" & year==2005,]

coal$highind=ifelse(coal$ayprodleaid>quantile(coal$ayprodleaid,0.5,na.rm=TRUE), 1,0)

coal$year=NULL

coal=merge(coal,dfpol,by=c("leaid"))
coal=coal[coal$testyear_cal<2017,]

dfavgplot=coal %>% group_by(highind,testyear_cal) %>% summarize(meanpm=mean(avgPM25_9))
dfavgplot$highlabel=ifelse(dfavgplot$highind=="1","High Coal","Low Coal")

ggplot(dfavgplot, aes(x=testyear_cal, y=meanpm,linetype=highlabel)) + geom_line(aes(linetype=highlabel), size=1.25) + geom_vline(xintercept=2011.5) + scale_y_continuous(breaks=seq(5,10,by=1),limits=c(5,10)) + scale_x_continuous( breaks = seq(2009, 2016, by = 1),labels=c("08-09","09-10","10-11","11-12","12-13","13-14","14-15","15-16")) + labs(linetype="Coal Status",x="Academic Year", y="Average PM2.5 by High & Low-Coal Districts", title=) + theme_classic() + theme(axis.text=element_text(size=22),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       axis.title=element_text(size=22,face="bold"),legend.position="bottom",legend.text = element_text(size = 20),text = element_text(size = 24))
# ########################################
ggsave("../output/Fig2a.pdf")

